/*
 * Copyright Aristool AG Switzerland
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

dojo.provide("opprua.widget.InfoCell");

dojo.require("dojox.layout.FloatingPane");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.Tree");
dojo.require("dijit.form.Button");

dojo.declare("opprua.widget.InfoCell", 
             dojox.layout.FloatingPane, { 
     widgetType:"InfoCell"
 ,   calls: []
 , showPane: function(name) {
    var w = this[name];
    if(name == "commPane") {
      for(i in this.calls) {
        w.containerNode.appendChild(this.calls[i].build());
      }
    } else if(name == "contentPane") {
      if(this.oldTree) {
        this.oldTree.destroy();
      }
      this.oldTree = opprua.registry.dump();
      var n = w.containerNode;
      while(n.firstChild) {
        n.removeChild(n.firstChild);
      }
	  w.containerNode.appendChild(this.oldTree.domNode);
    }
    dojo.forEach( ["commPane", "contentPane", "debugPane"], function(a) {
      dojo.byId(a).style.zIndex = 1;
    });
    w.domNode.style.zIndex = 1001;
	w.show();
}, clearLogs: function() {
	this.calls = [];
    dojo.forEach( ["commPane", "contentPane", "debugPane"], function(a) {
      dijit.byId(a).area.setContent("");
    });
}, debug: function(obj) {
    var d = document.createElement("div");
    d.innerHTML = "" + obj;
    this.debugPane.containerNode.appendChild(d);
}, callTrack: function(obj) {
    this.calls[this.calls.length] = new ictracker(obj);
    while(this.calls.length > 10) {
      this.calls.shift().destroy();
    }
}, addButton: function(cap, name) {
	var button = new dijit.form.Button({
	    label: "<div style='width:6em;border:0px'>" + cap + "</div>",
	    onClick: dojo.hitch(this, function(){
	            if(!name) {this.clearLogs()} else{this.showPane(name)}})});
	this.float.containerNode.appendChild(button.domNode);
	this.float.containerNode.appendChild(document.createElement("br"));
}, addPopup:function(id, title, p){
    var w = new dojox.layout.FloatingPane(
    { "id": id, title: "&nbsp;" + title, dockable: true, closable:false, resizable:true});
    this[id] = w;
    
    with(w.domNode.style) {
      width = "400px";
      height = "300px";
      position = "absolute";
	  top = "" + (20 + p * 40) + "px";
	  left = "" + (50 + p * 50) + "px";
      background = "rgb(210,210,220)";
      border = "0px";
      overflow = "hidden";
    }

    with(w.borderNode.style) {
      margin = "0px";
      top = "1.4em";
      left = "1px";
      right = "1px";
      bottom = "1px";
      position = "absolute";
    }

    with(w.containerNode.style) {
      width= "100%";
      height= "100%";
      overflow = "auto";
      background = "rgb(235,235,255)";
    }
    
    w.area = new dijit.layout.ContentPane({}, w.containerNode);
    document.body.appendChild(w.domNode);
    w.minimize = w.hide;
    w.startup();
	w.hide();
	
}, show:function() {
    this.float = new dojox.layout.FloatingPane( 
                     { title: "&nbsp;Info cell", docable: false, closable:false});
    document.body.appendChild(this.float.domNode);

    with (this.float.domNode.style) {
      background = "rgb(210,210,220)"
      border = "solid rgb(210,210,220) 2px";
      top = "20px";
      left = "700px";
      overflow = "hidden";
      position = "absolute";
    }
    
    this.addButton("show debug", "debugPane");
    this.addButton("show comm", "commPane");
    this.addButton("show content", "contentPane");
    this.addButton("clear logs", null);
    
    this.addPopup("debugPane", "Debugging log", 0);
    this.addPopup("commPane", "Communication log", 1)
    this.addPopup("contentPane", "Page content", 2)
    
    this.float.show();
}});

ictracker = function(obj) {
  this.data = obj;
  
  this.build = function() {
    if(this.div) return this.div;
    this.div = document.createElement("div");

    this.tease = document.createElement("div");
    this.tease.innerHTML = this.data.time + " -- " + this.data.toServer.entry;
    this.tease.className = "oppruaautolink";
    this.tease.onclick = dojo.hitch(this, "pop");
    this.div.appendChild(this.tease);
    
    return this.div;
  }
  
  this.pop = function() {
    if(this.show != null) {
      this.destroy();
    } else {
      this.show = document.createElement("div");
      this.tos = opprua.dumpObject(this.data.toServer, "toServer");
      this.froms = opprua.dumpObject(this.data.fromServer, "fromServer");
      this.show.appendChild(this.tos.domNode);
      this.show.appendChild(this.froms.domNode);
      this.show.style.marginLeft = "20px";
      this.div.appendChild(this.show);
    }
  }
  
  this.destroy = function() { 
    try {
      if(this.tos) this.tos.destroy();
      if(this.froms) this.froms.destroy();
      this.div.removeChild(this.show);
      this.show = null;
    } catch(e) {
    }
  }
}